package Client.GUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import Client.logic.ClientIF;
import Server.DataBase.Course;
import Server.DataBase.Lesson;
import Server.DataBase.Teacher;
import Server.Message.MessageGetAllCourses;
import Server.Message.MessageGetCoursesReplay;
import Server.Message.MessageGetLessonByCourse;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageSendRequest;
import Server.Message.MessageTeacherByUnit;
import Server.Message.MessageTeacherReplay;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class ChangeTeacherPanel extends MyJPanel {


	private static final long serialVersionUID = 1L;
	private JComboBox teachercomboBox;
	private JComboBox coursecomboBox;
	private JComboBox classcomboBox;
	private ArrayList<Course> courseArray =null;
	private ArrayList<Lesson> lessonArray=null;
	private ArrayList<Teacher> teacherArray=null;
	private Course course;
	private Lesson lesson;
	private Teacher teacher;
	private JButton btnreset;
	private JButton btnChange;
	private JLabel label;
	
	
	public ChangeTeacherPanel(ClientIF client) {
		super(PanelType.CHANGE_TEACHER_PANEL, client);
		setLayout(null);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"change teacher.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(35, 7, 445, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		init();
	}
		
	public void initArrays(){
		courseArray = new ArrayList<Course>();
		getClient().sendMsgToServer(new MessageGetAllCourses());
		MessageGetCoursesReplay rep = (MessageGetCoursesReplay) getClient().getMessageFromServer();
		courseArray = rep.getCourseArray();
		}		

	public void initComboBoxs(){
		teachercomboBox = new JComboBox();
		teachercomboBox.setBounds(293, 256, 145, 20);
		add(teachercomboBox);
		teachercomboBox.setEnabled(false);
		
		coursecomboBox = new JComboBox();
		coursecomboBox.setBounds(145, 119, 108, 20);
		add(coursecomboBox);
		
		classcomboBox = new JComboBox();
		classcomboBox.setBounds(145, 160, 108, 20);
		add(classcomboBox);
		classcomboBox.setEnabled(false);
		
		for (int i=0; i<=courseArray.size(); i++){
			if (i==0) coursecomboBox.addItem("Choose..");
			else coursecomboBox.addItem(courseArray.get(i-1));	
		}
		coursecomboBox.addActionListener(new CourseListener());
	}

	public void initButtons(){
		btnChange = new JButton("Send request");
		btnChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().sendMsgToServer(new MessageSendRequest(3, teacher.getIdUser(), lesson.getIdLesson()));
				JOptionPane.showMessageDialog((Component) getClient(), "Request has been send");
				getClient().swapFromBack(pushPanel());
			}
		});
		btnChange.setBounds(201, 305, 115, 23);
		add(btnChange);
		btnChange.setEnabled(false);
		
		btnreset = new JButton("reset");
		btnreset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().swapFromBack(pushPanel());
			}
		});
		btnreset.setBounds(145, 211, 108, 23);
		add(btnreset);
		btnreset.setEnabled(false);
		
	}

	public void initLables(){
		JLabel lblCourse = new JLabel("Course:");
		lblCourse.setBounds(89, 122, 46, 14);
		add(lblCourse);
		
		JLabel lblClass = new JLabel("Class:");
		lblClass.setBounds(89, 163, 46, 14);
		add(lblClass);
		
		label = new JLabel("");
		label.setBounds(89, 260, 164, 16);
		add(label);
		
	}


	public void init(){
		initArrays();
		initComboBoxs();
		initLables();
		initButtons();
	
	}
	
	private class CourseListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				course = (Course) coursecomboBox.getSelectedItem();
			
				getClient().sendMsgToServer(new MessageGetLessonByCourse(course.getIdCourse()));
				MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
				lessonArray = msg.getArray();
			
				for (int i=0; i<=lessonArray.size(); i++){
					if (i==0) classcomboBox.addItem("Choose..");
					else classcomboBox.addItem(lessonArray.get(i-1));
				}
				coursecomboBox.setEnabled(false);
				classcomboBox.setEnabled(true);
				btnreset.setEnabled(true);
				classcomboBox.addActionListener(new classListener());
			} catch (java.lang.Throwable t){}
		}
	}

	private class classListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				lesson = (Lesson) classcomboBox.getSelectedItem();
				getClient().sendMsgToServer(new MessageTeacherByUnit(lesson.getCourse().getTunit()));
				MessageTeacherReplay msg2 = (MessageTeacherReplay) getClient().getMessageFromServer();
				teacherArray = msg2.getArray();
			
				for (int i=0; i<=teacherArray.size(); i++){
					if (i==0) teachercomboBox.addItem("Choose..");
					else
						if (teacherArray.get(i-1).getIdUser()!=lesson.getTeacher().getIdUser())
							if (teacherArray.get(i-1).getMaxHours()>=(teacherArray.get(i-1).getHours()+lesson.getCourse().getHours()))
								teachercomboBox.addItem(teacherArray.get(i-1));
				}
				label.setText(lesson.getTeacher().getLastName()+" "+lesson.getTeacher().getFirstName());
				teachercomboBox.setEnabled(true);
				teachercomboBox.addActionListener(new teacherListener());
			} catch (java.lang.Throwable t){}
		}
	}
	
	private class teacherListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				teacher = (Teacher) teachercomboBox.getSelectedItem();
				btnChange.setEnabled(true);		
			} catch (java.lang.Throwable t){}
		}
	}

	public MyJPanel pushPanel() {
		return new ChangeTeacherPanel(getClient());
	}
}
